define(['jquery', 'bootstrap', 'backend', 'form', 'table'], function ($, undefined, Backend, Form, Table) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index/index',
                    add_url: 'order/index/add',
                    edit_url: 'order/index/edit',
                    del_url: 'order/index/del',
                    multi_url: 'order/index/multi',
                    detail_url: 'order/index/detail',
                    table: 'index'
                }
            });

            // 设置add、edit弹窗大小
            // $(".btn-add,.btn-editone").data("area", ['1000px','800px']);

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                showExport:false,
                columns: [
                    [
                        {field: 'state', checkbox: true},
                        {field: 'id', title: __('ID'),operate: false,sortable: true},
                        {field: 'order_sn', title: '订单号', sortable: false},
                        {field: 'customer_name', title: '客户', sortable: false},
                        {field: 'consignee', title: '收货人', sortable: false},
                        {field: 'mode', title: '交期方式', sortable: false},
                        {field: 'mobile', title: '送货电话', sortable: false},
                        {field: 'address', title: '送货地址', sortable: false},
                        {field: 'payment', title: '收款方式', sortable: false},
                        {field: 'status', title: __("Status"), searchList: {"1":"正常","-1":"异常"}, custom:{"1":"green","-1":"red"},formatter: Table.api.formatter.status},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
                            width: 150
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
                            width: 150
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: "load",
                                    text: "转生产单",//按钮名称
                                    // classname: 'btn btn-xs btn-success btn-magic ',
                                    classname: 'btn btn-xs btn-primary btn-magic btn-dialog',
                                    icon: 'fa fa-magic',
                                    url: 'produce/index/add?order_id={row.id}',//指向控制器对应方法
                                    confirm: '确认转发该订单变成车间生产单？',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: __('detail'),
                                    title: __('detail'),
                                    classname: 'btn btn-xs btn-success',
                                    url: $.fn.bootstrapTable.defaults.extend.detail_url,
                                },
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
            });


            // 打印
            $('.btn-print').click(function () {
                var ids = Table.api.selectedids(table);//获取选中列的id
                Fast.api.open("order/index/print?id="+ids, __('打印订单'), {
                    area: ['100%', '100%']
                });
            });
            // 为表格绑定事件
            Table.api.bindevent(table);


            // 为表格绑定事件
            Table.api.bindevent(table);

        },
        add: function () {

            $(document).on("fa.event.appendfieldlist", ".btn-append", function(){
                Form.events.selectpage($("#product_json"));
            });

            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("fa.event.appendfieldlist", ".btn-append", function(){
                Form.events.selectpage($("#product_json"));
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },

        },

    };


    return Controller;
});
